//
//  ShowMainViewController.m
//  HeadYang
//
//  Created by sunmingming on 15/8/6.
//  Copyright (c) 2015年 xiaoluoli. All rights reserved.
//

#import "ShowMainViewController.h"
#import "ShowDetailViewController.h"
#import "ShowMainCollectionViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "ShowAddViewController.h"
#import "MJRefresh.h"

@interface ShowMainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,ShowAddDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *cv;
@property(assign,nonatomic) BOOL refreshing;
@property(assign,nonatomic) NSInteger page;

@property (nonatomic,assign) int showMan;
@property (nonatomic,strong) IBOutlet UIButton *nanButton;
@property (nonatomic,strong) IBOutlet UIButton *nvButton;
@property (nonatomic,strong) IBOutlet UILabel *sexLabel;
-(IBAction)genderButtonPressed:(id)sender;

@property (nonatomic,assign) int hairType;
@property (nonatomic,strong) IBOutlet UIButton *typeButton1;
@property (nonatomic,strong) IBOutlet UIButton *typeButton2;
@property (nonatomic,strong) IBOutlet UIButton *typeButton3;
@property (nonatomic,strong) IBOutlet UIButton *typeButton4;
@property (nonatomic,strong) IBOutlet UIButton *typeButton5;
@property (nonatomic,strong) IBOutlet UIButton *typeButton6;
@property (nonatomic,strong) IBOutlet UIButton *typeButton7;
@property (nonatomic,strong) IBOutlet UIButton *typeButton8;
@property (nonatomic,strong) IBOutlet UIButton *typeButton9;
@property (nonatomic,strong) IBOutlet UIButton *typeButton10;
@property (nonatomic,strong) NSArray *typeButtonArray;
@property (nonatomic,strong) NSArray *typeStrArray;
-(IBAction)typePressed:(id)sender;

@property(nonatomic,strong) NSMutableArray *showArray;

@end

@implementation ShowMainViewController

@synthesize refreshing = _refreshing;
@synthesize page = _page;

@synthesize typeButton1,typeButton2,typeButton3,typeButton4,typeButton5,typeButton6,typeButton7,typeButton8,typeButton9,typeButton10;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"晒单";
        self.tabBarItem.image = [UIImage imageNamed:@"tabbaritem2"];
        self.showMan = 0;
        self.hairType = 1;
        self.typeStrArray = @[@"长发",@"中发",@"短发",@"直发",@"卷发",@"编发",@"潮流",@"明星",@"宴会",@"新娘"];
        self.showArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.typeButtonArray = @[typeButton1,typeButton2,typeButton3,typeButton4,typeButton5,typeButton6,typeButton7,typeButton8,typeButton9,typeButton10];
    UINib *cellNib = [UINib nibWithNibName:@"ShowMainCell" bundle:nil];
    [self.cv registerNib:cellNib forCellWithReuseIdentifier:@"ShowMainCell"];
    // Do any additional setup after loading the view from its nib.
    _page = 0;
    _refreshing = YES;
    self.cv.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.refreshing = YES;
        self.page = 0;
        [self getShowLists];
    }];
    self.cv.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.refreshing = NO;
        self.page++;
        [self getShowLists];
    }];
    [self setSex];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)genderButtonPressed:(id)sender {
    self.showMan = [sender tag] == 11?1:0;
    [self setSex];
}

-(IBAction)typePressed:(id)sender {
    self.hairType = [sender tag] - 100;
    [self setType];
}

-(void)setSex {
    if(self.showMan){
        self.sexLabel.backgroundColor = [UIColor blueColor];
        for(UIButton *button in self.typeButtonArray){
            [button setBackgroundImage:[UIImage imageNamed:@"select_back_male.png"] forState:UIControlStateNormal];
        }
        typeButton10.hidden = true;
        if(self.hairType == 10){
            self.hairType = 1;
        }
    }else{
        self.sexLabel.backgroundColor = [UIColor redColor];
        for(UIButton *button in self.typeButtonArray){
            [button setBackgroundImage:[UIImage imageNamed:@"select_back_female.png"] forState:UIControlStateNormal];
        }
        typeButton10.hidden = false;
    }
    [self setType];
}

-(void)setType {
    for(UIButton *button in self.typeButtonArray){
        if ([button tag] - 100 == self.hairType) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }else {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [button setTitle:[self.typeStrArray objectAtIndex:([button tag] - 101)] forState:UIControlStateNormal];
    }
    self.refreshing = YES;
    self.page = 0;
    [self getShowLists];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.showArray.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"ShowMainCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    NSDictionary *showDic = [self.showArray objectAtIndex:indexPath.row];
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:10];
    UIImageView *imageVie = (UIImageView*)[cell viewWithTag:11];
    NSDictionary *imgDic = [showDic objectForKey:@"mainPic"];
    [imageVie setImageWithURL:[NSURL URLWithString:[imgDic valueForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"default_photo.png"]];
    titleLabel.text = [NSString stringWithFormat:@"%@ ♡%d",[tooles replaceNullFromDic:showDic withKeyArray:@[@"title"]],[[showDic valueForKey:@"praise"] intValue]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *showDic = [self.showArray objectAtIndex:indexPath.row];
    APPALL.showID = [showDic valueForKey:@"id"];
    ShowDetailViewController *vc = [ShowDetailViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getShowLists{
    NSDictionary *para =
    @{@"gender":self.showMan?@"男":@"女",
      @"hairType":[self.typeStrArray objectAtIndex:(self.hairType - 1)],
      @"customer":@"",
      @"max":@"21",
      @"offset":[NSString stringWithFormat:@"%i",_page * 21],
      @"goods3":@"",
      @"goods4":@"",
      @"goods5":@"",
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"show_find" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        [self.cv.header endRefreshing];
        [self.cv.footer endRefreshing];
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
                    if(_refreshing){
                        [_showArray removeAllObjects];
                    }
                    NSArray *cityArray = [aSucData objectForKey:@"show"];
                    for (NSDictionary *dic in cityArray) {
                        [_showArray addObject:dic];
                    }
                    [self.cv reloadData];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)showAdd{
    ShowAddViewController *vc = [ShowAddViewController new];
    vc.delegate = self;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)addFinished:(NSString *)result{
    [self.cv.header beginRefreshing];
    [self.navigationController popViewControllerAnimated:YES];
}

@end